<template lang="">
    <div>
  <el-table :data="userlist" border style="width: 100%">
    <el-table-column  prop="uid" label="用户编号" width="240"></el-table-column>
    <el-table-column prop="username" label="用户名称" width="130"> </el-table-column>
     <el-table-column prop="rolename" label="所属角色" width="130"> </el-table-column>
      <el-table-column  label="状态"> 
     <template slot-scope="item">
    <div>
        <el-tag v-if="item.row.status == 1 " type="success">启用</el-tag>
        <el-tag v-else type="danger">禁用</el-tag>
        <!-- {{item.row.status == 1 ? "启用" : "禁用" }} -->
    </div>
  </template>
   </el-table-column>
     <el-table-column  label="操作">
        <template slot-scope="item">
          <div>
            <el-button type="primary"  size="mini" @click="edit(item.row.uid)" >编辑</el-button>
        <el-button type="danger" size="mini" @click="del(item.row.uid)" >删除</el-button>
          </div>
        </template>
     </el-table-column>
  </el-table>
   <!-- 分页组件 
    background	是否为分页按钮添加背景色
    page-size	每页显示条目个数，支持 .sync 修饰符  	10
    total	总条目数
    layout	组件布局，子组件名用逗号分隔
    sizes, prev, pager, next, jumper, ->, total, slot
    current-change currentPage 改变时会触发
    -->
  <el-pagination :page-size="getSize" @current-change="changePage" class="pagination" background layout="prev, pager, next":total="total"></el-pagination>
    </div>
</template>
<script>
import { mapActions, mapGetters } from 'vuex'
import { userDel } from '../../request/api'
export default {
  computed: {
    ...mapGetters({
      userlist: 'user/getUserList',
      total: 'user/getCount',
      getSize: 'user/getSize'
    })
  },
  mounted() {
    //页面一加载获取管理员列表
    this.getUser()
    //总条数的获取
    this.getCount()
  },
  methods: {
    //封装一个切换页码的事件
    changePage(n) {
      console.log(n, '当前页');
      this.getUserPageAction(n)
    },
    ...mapActions({
      getUser: 'user/changeUserAction',
      getCount: 'user/getCountAction',
      getUserPageAction: 'user/getUserPageAction'
    }),
    // 点击编辑事件
    edit(id) {
      this.$emit('edit',id)
    },
    // 删除事件
    del(uid) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        userDel({
          uid
        })
        .then((res)=>{
            if(res.code==200){
              this.$message.success(res.msg);
              //删除成功之后我们要重新调用列表以及总条数
              this.getUser()
              this.getCount()
            }else{
              this.$message.error(res.msg)
            }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  },
}
</script>
<style lang="less" scoped>
.pagination {
  float: right
}
</style>